﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.DataAccess;
using BySoft.Server.Common;

namespace BySoft.Server.Business 
{
    public partial class SysFormcheck
    {
        public static void SaveOrUpdate(SysFormcheckEntity Entity, string Type, List<SysFormcheckdetailEntity> lstbody, DataTable dtFormCheckData)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (Type == "Save")
                    {
                        DataAccessCommon.Insert(Entity, true, broker);
                        for (int i = 0; i < lstbody.Count; i++)
                        {
                            lstbody[i].FormCheckID = Entity.FormCheckID;
                            DataAccessCommon.Insert(lstbody[i], true, broker);
                        }
                    }
                    else
                    {
                        DataAccessCommon.UpdateOnly(Entity, broker);
                        foreach (DataRow drItem in dtFormCheckData.Rows)
                        {
                            string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                            string strIsDel = CommonMethod.GetString(drItem["IsDel"]);

                            string strFormCheckDetailID = CommonMethod.GetString(drItem["FormCheckDetailID"]);
                            if (strIsDel == "1" && strIsNew == "-1" )
                            {
                                ClsCommDA.Del("SYS_FormCheckDetail", "FormCheckDetailID", strFormCheckDetailID, broker);
                            }
                            else if (strIsDel == "-1" && strIsNew == "-1" )
                            {
                                SysFormcheckdetailEntity entbody = new SysFormcheckdetailEntity();
                                entbody.FormCheckDetailID = CommonMethod.ConvertToDecimal(strFormCheckDetailID);
                                entbody.CheckUser = CommonMethod.GetString(drItem["CheckUser"]);
                                entbody.OrderNum = CommonMethod.ConvertToInt(drItem["OrderNum"]);
                                entbody.IsEnd = CommonMethod.GetString(drItem["IsEnd"]);
                                DataAccessCommon.UpdateOnly(entbody, broker);
                            }
                            else if (strIsDel == "-1" && strIsNew == "1" )
                            {
                                SysFormcheckdetailEntity entbody = new SysFormcheckdetailEntity();
                                entbody.FormCheckID = Entity.FormCheckID;
                                entbody.CheckUser = CommonMethod.GetString(drItem["CheckUser"]);
                                entbody.OrderNum = CommonMethod.ConvertToInt(drItem["OrderNum"]);
                                entbody.IsEnd = CommonMethod.GetString(drItem["IsEnd"]);
                                DataAccessCommon.Insert(entbody, true, broker); 
                            }
                        }
                    }
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static DataTable SelectList(string strCode)
        {
            return SysFormcheckDA.SelectList(strCode);
        }

        public static DataTable SelectHead(string strCode)
        {
            return SysFormcheckDA.SelectHead(strCode);
        }

    }
}
